package common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * 工具类操作String类型的公共方法类
 */
public class StringUtil {
    /**
     * 判断字符串开头是否为中文
     */
    public static boolean isChinese(String str) {
        char[] strChar = str.toCharArray();
        char a = strChar[0];
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(a);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否为空
     */
    public static boolean nullFlag(String str) {
        if (str != null && !str.equals("")) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isNotNull(String str){
        if(str!=null&&!str.equals("")){
            return true;
        }else{
            return false;
        }
    }


    /**
     * encodeUrl
     */
    public static String encodeUrl(String url) {
        try {
            url = URLEncoder.encode(url, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return url;
    }

    /**
     * Unicode解密
     */
    public static String UnicodetoUTF(String str) {
        byte[] utf8 = new byte[0];
        try {
            utf8 = str.getBytes("UTF-8");
            str = new String(utf8, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String removeWos(String str) {
        str = str.replace("WOS:", "").replace("wos:", "").replace("WOS", "").replace("wos", "");
        return str;
    }

    public static String findDbName(String str) {
        String doM = "";
        List<String> split = Arrays.asList(str.split("/"));
        if (split.size() > 1) {
            String s = split.get(2);
            List<String> doMain = Arrays.asList(s.split("\\."));
            if (doMain.size() > 2) {
                doM = doMain.get(1) + "." + doMain.get(2);
            }
        }
        return doM;
    }

    public static boolean isNull(String str) {

        if (str != null && !str.equals("")) {
            return false;
        } else {
            return true;
        }
    }
}
